using System;
using System.Linq;
using System.Collections.Generic;

namespace SilenthinkContract.Domain
{
    /// <summary>
    /// 实行折扣必要条件
    /// </summary>
    [Serializable]
    public class DiscountRequirement : Versionable<int>
    {
        /// <summary>
        /// 折扣Id
        /// </summary>
        public int DiscountId { get; set; }
        /// <summary>
        /// 需要的条件名称
        /// </summary>
        public DiscountRequirementRule RequirementRule { get; set; }
        /// <summary>
        /// Gets or sets the the discount requirement spent amount - customer had spent/purchased x.xx amount (used when requirement is set to "Customer had spent/purchased x.xx amount")
        /// </summary>
        public decimal? SpentAmount { get; set; }
        /// <summary>
        /// Gets or sets the discount requirement - customer's billing country is... (used when requirement is set to "Billing country is")
        /// </summary>
        public string BillingAreaCodes { get; set; }
        /// <summary>
        /// Gets or sets the discount requirement - customer's shipping country is... (used when requirement is set to "Shipping country is")
        /// </summary>
        public string ShippingAreaCodes { get; set; }
        /// <summary>
        /// Gets or sets the restricted customer role identifier
        /// </summary>
        public string UserRoleIds { get; set; }
        /// <summary>
        /// Gets or sets the restricted product variant identifiers (comma separated)
        /// </summary>
        public string ProductVariantIds { get; set; }

        public Guid[] ProductVariantGuids
        {

            get
            {
                var result = new List<Guid>();
                if (string.IsNullOrEmpty(ProductVariantIds))
                {
                    return new Guid[0];
                }
                var array = ProductVariantIds.Split(',');
                result.AddRange(array.Select(s => new Guid(s)));
                return result.ToArray();
            }
        }
        public static IDictionary<byte, string> GetRequirementRuleDic()
        {
            var dic = new Dictionary<byte, string>();
            dic.Add(1, "已经消费过{0}元");
            dic.Add(2, "购买过{0}中的任何一个商品");
            dic.Add(3, "购买过{0}商品");
            dic.Add(4, "购物车中包含{0}中的任何一个商品");
            dic.Add(5, "购物车中包含{0}商品");
            dic.Add(6, "必须是{0}中的任何一个角色");
            dic.Add(7, "帐单地址为{0}中的任何一个");
            dic.Add(8, "收货地址为{0}中的任何一个");
            return dic;
        }

        public string GetRequirementRule()
        {
            var s = string.Empty;
            var rule = RequirementRule;

            switch (rule)
            {
                case DiscountRequirementRule.HadSpentAmount:
                    s = string.Format("已经消费过{0}元", (SpentAmount.HasValue ? SpentAmount.Value : 0).ToString("N")); break;
                case DiscountRequirementRule.PurchasedOneProduct:
                    s = string.Format("购买过{0}中的任何一个商品", ProductVariantIds); break;
                case DiscountRequirementRule.PurchasedAllProducts:
                    s = string.Format("购买过{0}商品", ProductVariantIds); break;
                case DiscountRequirementRule.HasOneProduct:
                    s = string.Format("购物车中包含{0}中的任何一个商品", ProductVariantIds); break;
                case DiscountRequirementRule.HasAllProducts:
                    s = string.Format("购物车中包含{0}商品", ProductVariantIds); break;
                case DiscountRequirementRule.UserRoles:
                    s = string.Format("必须是{0}中的任何一个角色", UserRoleIds); break;
                case DiscountRequirementRule.BillingAreaCodes:
                    s = string.Format("帐单地址为{0}中的任何一个", BillingAreaCodes); break;
                case DiscountRequirementRule.ShipppingAreaCodes:
                    s = string.Format("收货地址为{0}中的任何一个", ShippingAreaCodes); break;
            }
            return s;
        }
    }
}